// 权限控制
import router from './router'
import { getToken } from './utils/auth'
import store from './store'
const WHITE_LIST = ['/login', '/404']


// 处理一级路由权限数据
function getFirstRoutePerms(permsArr) {
    const newArr = permsArr.map(item => {
        return item.split(':')[0]
    })
    return [...new Set(newArr)]
}

// 处理二级路由权限数据
function getSecondRoutePerms(permsArr) {
    const newArr = permsArr.map(item => {
        const arr = item.split(':')
        return `${arr[0]}:${arr[1]}`
    })
    return [...new Set(newArr)]
}
router.beforeEach(async (to, from, next) => {
    const token = getToken('park_token')
    // 有token
    if (token) {
        next()
        // 获取用户信息
        if (!store.state.user.profile.id) {
            // 获取原始权限列表
            console.log(111111111111);
            const permissions = await store.dispatch('user/getProfile')
            console.log(permissions)
        }
    } else {
        console.log(2222222222222);
        // 没有token
        if (WHITE_LIST.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
})